<?php
class Text {
    public function getText($formatted = true) {
        $sql = "SELECT
                    Text
                FROM
                    texte
                WHERE
                    Name = '" . $this->viewPart . "'
                LIMIT 1";
        try {
            $result = $this->db->query($sql);
            $resultSet = $this->db->fetch_array($result);
            if ($formatted)
                return $this->formatText($resultSet['Text']);
            else
                return $resultSet['Text'];
        } catch (Exception $e) {
            #$this->db->raise_error();
            return false;
        }
    }

    private function formatText($text) {
        $text = explode("\n", $text);
        foreach ($text as $key => $value) {
            if (ereg("\[tabelle", $value))
                $return_text .= $this->bb_code($value);
            else {
                if (($key + 1) < count($text))
                    $return_text .= nl2br($this->bb_code($value) . "\n");
                else
                    $return_text .= $this->bb_code($value);
            }
        }
        return $return_text;
    }

    private function bb_code($text) {
        $search = array(
        '/\[hr]/siU',
        '/\[b](.*)\[\/b\]/siU',
        '/\[i](.*)\[\/i\]/siU',
        '/\[u](.*)\[\/u\]/siU',
        '/\[center](.*)\[\/center\]/siU',
        '/\[left](.*)\[\/left\]/siU',
        '/\[right](.*)\[\/right\]/siU',
        '/\[img](.*\.(jpg|jpeg|gif|png|bmp|tif|tiff))\[\/img\]/siU',
        '/\[img width=(.*) height=(.*)\](.*)\[\/img\]/siU',
        '/\[flash=(.*),(.*)\](.*)\[\/flash\]/siU',
        '/\[video=(.*),(.*)\](.*)\[\/video\]/siU',
        '#\[color=([\w|\#\d]*?)\](.*?)\[/color\]#si',
        '#\[size=([\w]*?)\](.*?)\[/size\]#si',
        '#\[url=(.*?)\](.*?)\[/url\]#si',
        '#\[url\]www.(.*?)\[/url\]#si',
        '#\[url\](.*?)\[/url]#si',
        '#\[email\](.*?)\[/email\]#si',
        '#\[tabelle\=2\](.*?)\|(.*?)\[/tabelle\]#si',
        '#\[tabelle\=3\](.*?)\|(.*?)\|(.*?)\[/tabelle\]#si',
        '#\[margin (.*?)\=(.*?)\](.*?)\[/margin\]#si',
        );
        $replace = array(
        '<hr />',
        '<strong>\\1</strong>',
        '<i>\\1</i>',
        '<u>\\1</u>',
        '<center>\\1</center>',
        '<div style="float:left;">\\1</div>',
        '<div style="float:right;">\\1</div><br style="clear:both;" />',
        '<img border="0" src="\\1" />',
        '<img style="border:0px;" width="\\1" height="\\2" src="\\3" />',
        '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="\\1" height="\\2"><param name="movie" value="\\3" /><param name="quality" value="high" /><param name="BGCOLOR" value="#FFCC66" /><embed src="\\3" width="\\1" height="\\2" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" bgcolor="#FFCC66"></embed></object>',
        '<object id="MediaPlayer1" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" standby="Loading Microsoft� Windows� Media Player components..." type="application/x-oleobject"><param name="AutoStart" value="True"><param name="FileName" value="\\3"><param name="ShowControls" value="True"><param name="ShowStatusBar" value="True"><embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="\\3" name="MediaPlayer1" width=\\1 height=\\2 autostart=1 showcontrols=0></embed></object>',
        '<span style="color:\\1">\\2</span>',
        '<span style="font-size:\\1">\\2</span>',
        '<a class="link" href="\\1" target="_blank">\\2</a>',
        '<a class="link" href="http://www.\\1" target="_blank">http://www.\\1</a>',
        '<a class="link" href="\\1" target="_blank">\\1</a>',
        '<a class="email" href="?site=contact">\\1</a>',
        '<table class="edit_table" width="100%" cellspacing="0" cellpadding="0"><tr><td width="33%">\\1</td><td width="50%">\\2</td><td width="17%">&nbsp;</td></tr></table>',
        '<table class="edit_table" width="100%" cellspacing="0" cellpadding="0"><tr><td width="55%">\\1</td><td width="15%" align="right">\\2</td><td width="30%" align="right">\\3</td></tr></table>',
        '<div style="margin-\\1:\\2px;">\\3</div>'
        );
        $text = preg_replace($search,$replace,$text);

        return $text;
    }

    public function updateText($viewPart, $text = '') {
        $sql = "UPDATE
                    texte
                SET
                    Text = '" . htmlentities($text, ENT_QUOTES, 'UTF-8') . "'
                WHERE
                    Name = '" . $viewPart . "'";
        try {
            $this->db->query($sql);
            return true;
        } catch (Exception $e) {
            #$this->db->raise_error();
            return false;
        }

    }
}
?>